<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Aviso extends CI_Controller {

	private $param;					//para listar las Avisos de la sede que opera cada operador
		
	// Constructor de la clase
	function __construct() {
		parent::__construct();

		$this->load->model('Model_Aviso');
		$this->load->library('avisoLib');

		$this->form_validation->set_message('required', 'Debe ingresar campo %s');
        $this->form_validation->set_message('my_validation', 'Existe otro registro con el mismo nombre');
        
        $perfil_id = $this->session->userdata('perfil_id');
        if ($perfil_id <> 4){
        	$sede_id = $this->session->userdata('sede_id');
        	$this->param = 'aviso.sede_id = '.$sede_id;
        }    
    }

	public function index() {
		$data['contenido'] = 'aviso/index';
		$data['titulo'] = 'Avisos';				
		$data['query'] = $this->Model_Aviso->all($this->param);
		
		$this->load->view('template', $data);
	}
	
	public function search() {
		$data['contenido'] = 'aviso/index';
		$data['titulo'] = 'Avisos';
		$value = $this->input->post('buscar');
		$data['query'] = $this->Model_Aviso->allFiltered('aviso.name', $value,$this->param);
		$this->load->view('template', $data);
	}

	public function my_validation() {
		return $this->avisolib->my_validation($this->input->post());
	}

	public function my_validate_time() {
		/* falta validad la fecha!!!!!!!!!!!!!!!!!!!!!!!*/
		return TRUE;
	}	

	public function create() {
		$data['contenido'] = 'aviso/create';
		$data['titulo'] = 'Crear Aviso';
		$data['responsables'] = $this->Model_Aviso->get_Responsables(); /* Lista de los responsables */
		$this->load->view('template', $data);
	}

	public function insert() {
		$registro = $this->input->post();
		$sede_id = $this->session->userdata('sede_id');
		$registro['sede_id'] = $sede_id;
		
		$this->form_validation->set_rules('descripcion', 'Descripci&oacute;n', 'required|callback_my_validation');
		$this->form_validation->set_rules('fecha', 'Fecha', 'required');
		$this->form_validation->set_rules('name', 'Nombre', 'required');
		$this->form_validation->set_rules('responsable_id', 'Responsable', 'required');			
        if ($this->form_validation->run() == FALSE) {
            $this->create();
        }
        else {
			$this->Model_Aviso->insert($registro);
			redirect('aviso/index/create_success');
        }
	}

	public function edit($id) {
		$data['contenido'] = 'aviso/edit';
		$data['titulo'] = 'Actualizar Aviso';
		$data['registro'] = $this->Model_Aviso->find($id);
		$data['responsables'] = $this->Model_Aviso->get_Responsables();
		$this->load->view('template', $data);
	}

	public function update() {
		$registro = $this->input->post();
		
		$this->form_validation->set_rules('descripcion', 'Descripci&oacute;n', 'required|callback_my_validation');
		$this->form_validation->set_rules('fecha', 'Fecha', 'required');
		$this->form_validation->set_rules('name', 'Nombre', 'required');
		$this->form_validation->set_rules('responsable_id', 'Responsable', 'required');			
				
		if($this->form_validation->run() == FALSE) {
			$this->edit($registro['id']);
		}
		else {
			$this->Model_Aviso->update($registro);
			redirect('aviso/index/success');
		}
	}
	
	public function delete($id) {
		$this->Model_Aviso->delete($id);
		redirect('aviso/index/delete_success');
	}

}
